Merge "Avoid global state in DatabaseBase::factory()/query()"
[lhc/web/wiklou.git] / includes / db / Database.php
index 232bd38..6c1e37f 100644 (file)
@@ -87,7 +87,7 @@ abstract class DatabaseBase implements IDatabase, LoggerAwareInterface {
        protected $mTrxPreCommitCallbacks = [];
        /** @var array[] List of (callable, method name) */
        protected $mTrxEndCallbacks = [];
-       /** @var array[] Map of (name => (callable, method name)) */
+       /** @var callable[] Map of (name => callable) */
        protected $mTrxRecurringCallbacks = [];
        /** @var bool Whether to suppress triggering of transaction end callbacks */
        protected $mTrxEndCallbacksSuppressed = false;
@@ -414,18 +414,25 @@ abstract class DatabaseBase implements IDatabase, LoggerAwareInterface {
         *   - false to disable debugging
         *   - omitted or null to do nothing
         *
-        * @return bool|null Previous value of the flag
+        * @return bool Previous value of the flag
+        * @deprecated since 1.28; use setFlag()
         */
        public function debug( $debug = null ) {
-               return wfSetBit( $this->mFlags, DBO_DEBUG, $debug );
+               $res = $this->getFlag( DBO_DEBUG );
+               if ( $debug !== null ) {
+                       $debug ? $this->setFlag( DBO_DEBUG ) : $this->clearFlag( DBO_DEBUG );
+               }
+
+               return $res;
        }
 
        public function bufferResults( $buffer = null ) {
-               if ( is_null( $buffer ) ) {
-                       return !(bool)( $this->mFlags & DBO_NOBUFFER );
-               } else {
-                       return !wfSetBit( $this->mFlags, DBO_NOBUFFER, !$buffer );
+               $res = !$this->getFlag( DBO_NOBUFFER );
+               if ( $buffer !== null ) {
+                       $buffer ? $this->clearFlag( DBO_NOBUFFER ) : $this->setFlag( DBO_NOBUFFER );
                }
+
+               return $res;
        }
 
        /**
@@ -441,7 +448,12 @@ abstract class DatabaseBase implements IDatabase, LoggerAwareInterface {
         * @return bool The previous value of the flag.
         */
        protected function ignoreErrors( $ignoreErrors = null ) {
-               return wfSetBit( $this->mFlags, DBO_IGNORE, $ignoreErrors );
+               $res = $this->getFlag( DBO_IGNORE );
+               if ( $ignoreErrors !== null ) {
+                       $ignoreErrors ? $this->setFlag( DBO_IGNORE ) : $this->clearFlag( DBO_IGNORE );
+               }
+
+               return $res;
        }
 
        public function trxLevel() {
@@ -453,11 +465,17 @@ abstract class DatabaseBase implements IDatabase, LoggerAwareInterface {
        }
 
        public function tablePrefix( $prefix = null ) {
-               return wfSetVar( $this->mTablePrefix, $prefix );
+               $old = $this->mTablePrefix;
+               $this->mTablePrefix = $prefix;
+
+               return $old;
        }
 
        public function dbSchema( $schema = null ) {
-               return wfSetVar( $this->mSchema, $schema );
+               $old = $this->mSchema;
+               $this->mSchema = $schema;
+
+               return $old;
        }
 
        /**
@@ -2662,23 +2680,23 @@ abstract class DatabaseBase implements IDatabase, LoggerAwareInterface {
                return false;
        }
 
-       final public function onTransactionResolution( callable $callback ) {
+       final public function onTransactionResolution( callable $callback, $fname = __METHOD__ ) {
                if ( !$this->mTrxLevel ) {
                        throw new DBUnexpectedError( $this, "No transaction is active." );
                }
-               $this->mTrxEndCallbacks[] = [ $callback, wfGetCaller() ];
+               $this->mTrxEndCallbacks[] = [ $callback, $fname ];
        }
 
-       final public function onTransactionIdle( callable $callback ) {
-               $this->mTrxIdleCallbacks[] = [ $callback, wfGetCaller() ];
+       final public function onTransactionIdle( callable $callback, $fname = __METHOD__ ) {
+               $this->mTrxIdleCallbacks[] = [ $callback, $fname ];
                if ( !$this->mTrxLevel ) {
                        $this->runOnTransactionIdleCallbacks( self::TRIGGER_IDLE );
                }
        }
 
-       final public function onTransactionPreCommitOrIdle( callable $callback ) {
+       final public function onTransactionPreCommitOrIdle( callable $callback, $fname = __METHOD__ ) {
                if ( $this->mTrxLevel ) {
-                       $this->mTrxPreCommitCallbacks[] = [ $callback, wfGetCaller() ];
+                       $this->mTrxPreCommitCallbacks[] = [ $callback, $fname ];
                } else {
                        // If no transaction is active, then make one for this callback
                        $this->startAtomic( __METHOD__ );
@@ -2694,7 +2712,7 @@ abstract class DatabaseBase implements IDatabase, LoggerAwareInterface {
 
        final public function setTransactionListener( $name, callable $callback = null ) {
                if ( $callback ) {
-                       $this->mTrxRecurringCallbacks[$name] = [ $callback, wfGetCaller() ];
+                       $this->mTrxRecurringCallbacks[$name] = $callback;
                } else {
                        unset( $this->mTrxRecurringCallbacks[$name] );
                }
@@ -2809,9 +2827,8 @@ abstract class DatabaseBase implements IDatabase, LoggerAwareInterface {
                /** @var Exception $e */
                $e = null; // first exception
 
-               foreach ( $this->mTrxRecurringCallbacks as $callback ) {
+               foreach ( $this->mTrxRecurringCallbacks as $phpCallback ) {
                        try {
-                               list( $phpCallback ) = $callback;
                                $phpCallback( $trigger, $this );
                        } catch ( Exception $ex ) {
                                call_user_func( $this->errorLogger, $ex );
@@ -2898,7 +2915,7 @@ abstract class DatabaseBase implements IDatabase, LoggerAwareInterface {
                $this->mTrxAutomatic = ( $mode === self::TRANSACTION_INTERNAL );
                $this->mTrxAutomaticAtomic = false;
                $this->mTrxAtomicLevels = [];
-               $this->mTrxShortId = wfRandomString( 12 );
+               $this->mTrxShortId = sprintf( '%06x', mt_rand( 0, 0xffffff ) );
                $this->mTrxWriteDuration = 0.0;
                $this->mTrxWriteQueryCount = 0;
                $this->mTrxWriteAdjDuration = 0.0;
@@ -3518,15 +3535,18 @@ abstract class DatabaseBase implements IDatabase, LoggerAwareInterface {
                                // There is a good chance an exception was thrown, causing any early return
                                // from the caller. Let any error handler get a chance to issue rollback().
                                // If there isn't one, let the error bubble up and trigger server-side rollback.
-                               $this->onTransactionResolution( function () use ( $lockKey, $fname ) {
-                                       $this->unlock( $lockKey, $fname );
-                               } );
+                               $this->onTransactionResolution(
+                                       function () use ( $lockKey, $fname ) {
+                                               $this->unlock( $lockKey, $fname );
+                                       },
+                                       $fname
+                               );
                        } else {
                                $this->unlock( $lockKey, $fname );
                        }
                } );
 
-               $this->commit( __METHOD__, self::FLUSHING_INTERNAL );
+               $this->commit( $fname, self::FLUSHING_INTERNAL );
 
                return $unlocker;
        }